﻿using HuiLian.Cloud.Controls;
using HuiLian.Cloud.ViewModel;
using HuiLian.Cloud.View;
using HuiLian.Cloud.ViewModel.Base;
using HuiLian.Model.Models;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Collections.Specialized;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;

namespace HuiLian.Cloud.ViewModel
{
    public class MainWindowViewModel : ViewModelBase
    {
        private readonly static List<Food> sources = new List<Food>()
        {
            new Food(){ Id=1,Name="黑米面包", No="1", Image="Images/Foods/1.jpg", Price=2.5, Stock=100, Type="面包" },
            new Food(){ Id=2,Name="抹茶面包", No="2", Image="Images/Foods/2.jpg", Price=2.5, Stock=100, Type="面包" },
            new Food(){ Id=3,Name="奶油面包", No="3", Image="Images/Foods/3.jpg", Price=12.5, Stock=100, Type="面包" },
            new Food(){ Id=4,Name="蛋糕面包", No="4", Image="Images/Foods/4.jpg", Price=12.5, Stock=100, Type="面包" },
            new Food(){ Id=5,Name="葡萄奶茶", No="5", Image="Images/Foods/5.jpg", Price=22.5, Stock=100, Type="奶茶" },
            new Food(){ Id=6,Name="草莓奶茶", No="6", Image="Images/Foods/6.jpg", Price=12.5, Stock=100, Type="奶茶" },
            new Food(){ Id=7,Name="青梅奶茶", No="7", Image="Images/Foods/7.jpg", Price=23.5, Stock=100, Type="奶茶" },
            new Food(){ Id=8,Name="奶昔奶茶", No="8", Image="Images/Foods/8.jpg", Price=12.5, Stock=100, Type="奶茶" },
            new Food(){ Id=9,Name="蛋糕一", No="9", Image="Images/Foods/9.jpg", Price=12.5, Stock=100, Type="生日蛋糕" },
            new Food(){ Id=10,Name="蛋糕二", No="10", Image="Images/Foods/10.jpg", Price=20.5, Stock=100, Type="生日蛋糕" },
            new Food(){ Id=11,Name="蛋糕三", No="11", Image="Images/Foods/11.jpg", Price=26.5, Stock=100, Type="生日蛋糕" },
            new Food(){ Id=12,Name="蛋糕四", No="12", Image="Images/Foods/12.jpg", Price=82.5, Stock=100, Type="生日蛋糕" },
            new Food(){ Id=13,Name="炸鸡", No="13", Image="Images/Foods/13.jpg", Price=12.5, Stock=100, Type="汉堡炸鸡" },
            new Food(){ Id=14,Name="炸鸡", No="14", Image="Images/Foods/14.jpg", Price=1.5, Stock=100, Type="汉堡炸鸡" },
            new Food(){ Id=15,Name="炸鸡", No="15", Image="Images/Foods/15.jpg", Price=12.5, Stock=100, Type="汉堡炸鸡" },
            new Food(){ Id=16,Name="炸鸡", No="16", Image="Images/Foods/16.jpg", Price=12.5, Stock=100, Type="汉堡炸鸡" }
        };

        private List<Food> foods = new List<Food>(sources);

        public MainWindowViewModel()
        {

        }

        /// <summary>
        /// 食品列表
        /// </summary>
        public List<Food> Foods
        {
            get { return foods; }
            set { foods = value; OnPropertyChanged("Foods"); }
        }

        // ObservableCollection
        private ObservableCollection<OrderItem> orderItems = new ObservableCollection<OrderItem>();

        /// <summary>
        /// 订单列表
        /// </summary>
        public ObservableCollection<OrderItem> OrderItems
        {
            get { return orderItems; }
            set
            {
                orderItems = value;
                OnPropertyChanged("OrderItems");
            }
        }

        private OrderItem selectItem;

        public OrderItem SelectItem
        { 
            get { return selectItem; } 
            set 
            { 
                selectItem = value;
                OnPropertyChanged("SelectItem");
            } 
        }

        /// <summary>
        /// 获取总价
        /// </summary>
        /// <param name="orderItems"></param>
        /// <returns></returns>
        public string GetTotalAmount(ObservableCollection<OrderItem> orderItems)
        {
            double totalAmount = 0;
            if (orderItems != null && orderItems.Count > 0)
            {
                foreach (OrderItem item in orderItems)
                {
                    totalAmount += item.Count * item.Money;
                }
                return "￥：" + totalAmount.ToString();
            }
            return "￥：0";
        }

        /// <summary>
        /// 获取总数
        /// </summary>
        /// <param name="orderItems"></param>
        /// <returns></returns>
        public int GetTotalCount(ObservableCollection<OrderItem> orderItems)
        {
            int totalCount = 0;
            if (orderItems != null && orderItems.Count > 0)
            {
                foreach (OrderItem item in orderItems)
                {
                    totalCount += item.Count;
                }
                return totalCount;
            }
            return 0;
        }

        private string totalAmount = "￥：0";

        /// <summary>
        /// 总金额
        /// </summary>
        public string TotalAmount
        {
            get { return totalAmount; }
            set { totalAmount = value; OnPropertyChanged("TotalAmount"); }
        }

        private int totalCount = 0;

        /// <summary>
        /// 总数量
        /// </summary>
        public int TotalCount
        {
            get { return totalCount; }
            set { totalCount = value; OnPropertyChanged("TotalCount"); }
        }

        #region Commmand
        /// <summary>
        /// 交接班
        /// </summary>
        public ICommand OnShiftClickCommand => new DelegateCommand((sender) =>
        {
            ExchangeWindow shiftWindow = new ExchangeWindow();
            shiftWindow.Show();
        });

        /// <summary>
        /// 订单
        /// </summary>
        public ICommand OnOrdersClickCommand => new DelegateCommand((sender) => 
        {
            OrdersWindow ordersWindow = new OrdersWindow();
            ordersWindow.Show();
        });

        /// <summary>
        /// 出品
        /// </summary>
        public ICommand OnProductClickCommand => new DelegateCommand((sender) =>
        {
            ProductListWindow productListWindow = new ProductListWindow();
            productListWindow.Show();
        });

        /// <summary>
        /// 产品损耗
        /// </summary>
        public ICommand OnWastageClickCommand => new DelegateCommand((sender) =>
        {
            WastageWindow wastageWindow = new WastageWindow();
            wastageWindow.Show();
        });

        /// <summary>
        /// 每日盘点
        /// </summary>
        public ICommand OnCheckedClickCommand => new DelegateCommand((sender) =>
        {
            CheckedWindow checkedWindow = new CheckedWindow();
            checkedWindow.Show();
        });

        /// <summary>
        /// 开始营业
        /// </summary>
        public ICommand OnStartClickCommand => new DelegateCommand((sender) =>
        {
            OnBusinessWindow onBusinessWindow = new OnBusinessWindow();
            onBusinessWindow.Show();
        });

        /// <summary>
        /// 存单按钮
        /// </summary>
        public ICommand OnSaveClickCommand => new DelegateCommand((sender) =>
        {
            if (OrderItems != null)
            {
                WarningWindow messageWindow = new WarningWindow();
                messageWindow.Show();
            }
        });

        /// <summary>
        /// 取单按钮
        /// </summary>
        public ICommand OnTakeClickCommand => new DelegateCommand((sender) =>
        {
            if (OrderItems != null)
            {
                DepositListWindow depositListWindow = new DepositListWindow();
                depositListWindow.Show();
            }
        });

        /// <summary>
        /// 清空按钮
        /// </summary>
        public ICommand OnClearClickCommand => new DelegateCommand((sender) =>
        {
            this.OrderItems.Clear();
            this.SelectItem = null;
        });

        /// <summary>
        /// 口味按钮
        /// </summary>
        public ICommand OnSaborClickCommand => new DelegateCommand((sender) =>
        {
            if (this.SelectItem != null)
            {
                FlavourWindow flavourWindow = new FlavourWindow();
                if (flavourWindow.DataContext is FlavourWindowViewModel viewModel)
                {
                    viewModel.FoodName = this.SelectItem.Name;
                }
                flavourWindow.ShowDialog();
            }
        });

        /// <summary>
        /// 数量按钮
        /// </summary>
        public ICommand OnNumberClickCommand => new DelegateCommand((sender) =>
        {
            if (this.SelectItem != null)
            {
                NumberWindow numberWindow = new NumberWindow();
                if (numberWindow.DataContext is NumberWindowViewModel viewModel)
                {
                    viewModel.FoodName = this.SelectItem.Name;
                }
                numberWindow.ShowDialog();
            }
        });

        /// <summary>
        /// 折扣按钮
        /// </summary>
        public ICommand OnDiscountClickCommand => new DelegateCommand((sender) =>
        {
            DiscountWindow discountWindow = new DiscountWindow();
            discountWindow.ShowDialog();
        });

        /// <summary>
        /// 改价按钮
        /// </summary>
        public ICommand OnUpdateClickCommand => new DelegateCommand((sender) =>
        {
            PriceWindow priceWindow = new PriceWindow();
            priceWindow.ShowDialog();
        });

        /// <summary>
        /// 删除按钮
        /// </summary>
        public ICommand OnDeleteClickCommand => new DelegateCommand((sender) =>
        {
            for(int i= 0;i<OrderItems.Count;i++)
            {
                if (this.SelectItem != null && this.OrderItems[i].FoodId == this.SelectItem.FoodId)
                {
                    this.OrderItems.RemoveAt(i);
                    this.TotalAmount = this.GetTotalAmount(this.OrderItems);
                    this.TotalCount = this.GetTotalCount(this.OrderItems);
                    this.SelectItem = null;
                }
            }
        });

        /// <summary>
        /// 验证按钮
        /// </summary>
        public ICommand OnRechargeClickCommand => new DelegateCommand((sender) =>
        {
            VerifyWindow confrimWindow = new VerifyWindow();
            confrimWindow.ShowDialog();
        });

        /// <summary>
        /// 会员按钮
        /// </summary>
        public ICommand OnVipClickCommand => new DelegateCommand((sender) =>
        {
            MemberWindow vipUsersWindow = new MemberWindow();
            vipUsersWindow.ShowDialog();
        });

        /// <summary>
        /// 类别按钮
        /// </summary>
        public ICommand OnTypeClickCommand => new DelegateCommand((sender) =>
        {
            if (sender is GreenRadioButton button)
            {
                Foods = sources.Where((food) => button.Text.Equals("全部") || food.Type.Equals(button.Text)).ToList();
            }
        });

        /// <summary>
        /// 结账按钮
        /// </summary>
        public ICommand OnAccountClickCommand => new DelegateCommand((sender) =>
        {
            AccountWindow accountWindow = new AccountWindow();
            accountWindow.ShowDialog();
        });

        /// <summary>
        /// 商品点击事件
        /// </summary>
        public ICommand OnFoodClickCommand => new DelegateCommand((sender) =>
        {
            if (sender is Food food)
            {
                OrderItem item = new OrderItem() { Name = food.Name, Count = 1, Money = food.Price, FoodId = food.Id };
                for (int i = 0; i < OrderItems.Count; i++)
                {
                    if (OrderItems[i].FoodId.Equals(item.FoodId))
                    {
                        OrderItems[i].Count += 1;
                        TotalAmount = GetTotalAmount(OrderItems);
                        TotalCount = GetTotalCount(OrderItems);
                        return;
                    }
                }
                OrderItems.Add(item);
                TotalAmount = GetTotalAmount(OrderItems);
                TotalCount = GetTotalCount(OrderItems);
            }
        });

        /// <summary>
        /// 选中事件
        /// </summary>
        public ICommand OnSelectClickCommand => new DelegateCommand((sender) =>
        {
            if (sender is OrderItem orderItem)
            {
                SelectItem = orderItem;
            }
        });

        /// <summary>
        /// 加号按钮
        /// </summary>
        public ICommand OnPlusClickCommand => new DelegateCommand((sender) =>
        {
            if (sender is OrderItem orderItem)
            {
                OrderItem item = new OrderItem() { Name = orderItem.Name, Count = 1, Money = orderItem.Money, FoodId = orderItem.FoodId };
                for (int i = 0; i < OrderItems.Count; i++)
                {
                    if (OrderItems[i].FoodId.Equals(item.FoodId))
                    {
                        OrderItems[i].Count += 1;
                        TotalAmount = GetTotalAmount(OrderItems);
                        TotalCount = GetTotalCount(OrderItems);
                        return;
                    }
                }
                OrderItems.Add(item);
                TotalAmount = GetTotalAmount(OrderItems);
                TotalCount = GetTotalCount(OrderItems);
            }
        });

        /// <summary>
        /// 减号按钮
        /// </summary>
        public ICommand OnMinusClickCommand => new DelegateCommand((sender) =>
        {
            if (sender is OrderItem orderItem)
            {
                OrderItem item = new OrderItem() { Name = orderItem.Name, Count = 1, Money = orderItem.Money, FoodId = orderItem.FoodId };
                for (int i = 0; i < OrderItems.Count; i++)
                {
                    if (OrderItems[i].FoodId.Equals(item.FoodId))
                    {
                        if (OrderItems[i].Count <= 1)
                        {
                            if (OrderItems[i].FoodId == SelectItem?.FoodId)
                            {
                                SelectItem = null;
                            }
                            OrderItems.RemoveAt(i);
                        }
                        else
                        {
                            OrderItems[i].Count -= 1;
                        }
                        TotalAmount = GetTotalAmount(OrderItems);
                        TotalCount = GetTotalCount(OrderItems);
                        return;
                    }
                }
            }
        });
        #endregion
    }
}
